package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.IntegralExMapper;
import com.jieshuibao.mapper.IntegralMapper;
import com.jieshuibao.model.db.Integral;
import com.jieshuibao.model.db.IntegralExample;
import com.jieshuibao.model.domain.Integrals;

@Service
public class IntegralService{
	
	@Resource
	private IntegralMapper integralMapper;
	
	@Resource
	private IntegralExMapper integralExMapper;
	
	public int getIntegralScoreByUuid(String uuid){
		return integralMapper.selectScoreByUuid(uuid);
	}
	
	public void addIntegral(Integral integral) {
		integralMapper.insertSelective(integral);
	}
	
	public void delIntegralByUuid(String uuid) {
		integralMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateIntegral(Integral integral) {
		integralMapper.updateByPrimaryKeySelective(integral);
	}
	
	public Integral getIntegralByUuid(String uuid) {
		return integralMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Integral> getPageIntegral(IntegralExample integralExample) {
		return integralMapper.selectPage(integralExample);
	}
	
	public int countIntegral(IntegralExample integralExample) {
		return integralMapper.countByExample(integralExample);
	}
	
	public Integrals getIntegralsByUuid(String userId){
		return integralExMapper.selectByPrimaryKey(userId);
	}
	
	public void  updateUserIntegral(Integral integral){
		integralExMapper.updateByPrimaryKeySelective(integral);
		
	}
	
	//完善资料加积分
	public void integralCompleteData(String userId){
		Integrals integrals = integralExMapper.selectByPrimaryKey(userId);
		if (integrals==null) {
			Integrals integrals2 = new Integrals();
			integrals2.setUuid(userId);
			integrals2.setRegist(1);
			integrals2.setComplete(5);
			integralMapper.insertSelective(integrals2);
		}else {
			Integer complete = integrals.getComplete();
			if (complete==null||complete<=0) {
				Integrals integrals2 = new Integrals();
				integrals2.setUuid(userId);
				integrals2.setComplete(5);
				integralExMapper.updateByPrimaryKeySelective(integrals2);
			}
		}
	}
	
	// 注册加积分
	
	public void integralRegist(String userId){
		Integrals integrals = integralExMapper.selectByPrimaryKey(userId);
		if (integrals==null) {
			Integrals integrals2 = new Integrals();
			integrals2.setUuid(userId);
			integrals2.setRegist(1);
			integralMapper.insertSelective(integrals2);
		}
	}
	/**
	 * 
	 * @param userId 用户id
	 * @param type 类型
	 * @param score  分数
	 */
	public  void integral(String userId,String type,int score){
		Integrals integrals = integralExMapper.selectByPrimaryKey(userId);
		Integrals integrals2 = new Integrals();
		if (integrals==null) {
			integrals2.setUuid(userId);
			integrals2.setRegist(1);
			integralMapper.insertSelective(integrals2);
		}
		
		integrals2.setUuid(userId);
		if (type.equals("adop")) {
			integrals2.setAdop(1);
			integralExMapper.updateByPrimaryKeySelective(integrals2);
		}else if (type.equals("question")) {
			if (integrals!=null) {
				Integer question = integrals.getQuestion();
				if (question==null||question==0) {
					integrals2.setQuestion(5);
					integralExMapper.updateByPrimaryKeySelective(integrals2);
				}
			}else {
				integrals2.setQuestion(5);
				integralExMapper.updateByPrimaryKeySelective(integrals2);
			}
		}else if (type.equals("secretly")) {
			integrals2.setSecretly(1);
			integralExMapper.updateByPrimaryKeySelective(integrals2);
		}else if (type.equals("complete")) {
			if (integrals!=null) {
				Integer complete = integrals.getComplete();
				if (complete==null||complete<=0) {
					integrals2.setComplete(5);
					integralExMapper.updateByPrimaryKeySelective(integrals2);
				}
			}else {
				integrals2.setComplete(5);
				integralExMapper.updateByPrimaryKeySelective(integrals2);
			}
			
		}else if (type.equals("video")) {
			integrals2.setVideo(1);
			integralExMapper.updateByPrimaryKeySelective(integrals2);
		}else if (type.equals("recharge")) {
			integrals2.setRecharge(score);
			integralExMapper.updateByPrimaryKeySelective(integrals2);
		}
		
		
	}
	/**
	 * 查看用户是否满足相关积分
	 * @param userId
	 * @param level 级别分数
	 * @return
	 */
	public boolean integralLavel(String userId,int level){
		Integrals integrals = integralExMapper.selectByPrimaryKey(userId);
		if (integrals==null) {
			return false;
		}else {
			int integralSum = integrals.getIntegralSum();
			if (integralSum<level) {
				return false;
			}else {
				return true;
			}
		}
	}
}
